package com.Yukiha.gachamod.gachaMenu.client.gui;

import com.Yukiha.gachamod.gachaMenu.ModMenuTypes;
import com.Yukiha.gachamod.gachaMenu.client.ClientGachaDataCache;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;

import com.Yukiha.gachamod.Data.GachaSavedData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

import java.util.List;
import java.util.UUID;

public class GachaBookMenu extends AbstractContainerMenu {
    private final List<String> playerModels;
    private int page = 0;
    private final int modelsPerPage = 8;

    public GachaBookMenu(int containerId, Inventory inv, UUID playerUUID) {
        super(ModMenuTypes.GACHA_BOOK_MENU.get(), containerId);
        Level level = inv.player.level();
        if (!(level instanceof ServerLevel serverLevel)) {
            throw new IllegalStateException("GachaBookMenu 只能在服务端构造");
        }
        this.playerModels = List.copyOf(GachaSavedData.get(serverLevel).getModels(playerUUID));
    }

    public GachaBookMenu(MenuType<?> type, int containerId, Inventory inv, FriendlyByteBuf buffer) {
        super(type, containerId);
        this.playerModels = ClientGachaDataCache.getPlayerModelList();
    }


    public List<String> getPlayerModels() {
        return playerModels;
    }

    public void nextPage() {
        if ((page + 1) * modelsPerPage < playerModels.size()) {
            page++;
        }
    }

    public void previousPage() {
        if (page > 0) {
            page--;
        }
    }

    public int getCurrentPage() {
        return page + 1;
    }

    public int getMaxPages() {
        return (int) Math.ceil((double) playerModels.size() / modelsPerPage);
    }

    public int getPage() {
        return this.page;
    }

    @Override
    public ItemStack quickMoveStack(Player player, int index) {
        return ItemStack.EMPTY;
    }

    @Override
    public boolean stillValid(Player player) {
        return true;
    }
}